package com.cljz.peipei.utils.date;

import android.annotation.SuppressLint;
import android.content.Context;
import android.telephony.TelephonyManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 时间类
 */
public class GetSystemTimeUtil {

    /**
     * 包含年月日时分秒
     */
    @SuppressLint("SimpleDateFormat")
    public static String getInDetailgetTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        return time;
    }

    /**
     * 包含时分
     */
    @SuppressLint("SimpleDateFormat")
    public static String getNowTurnTime(String string) {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        String time = df.format(getDate(string));
        return time;
    }

    /**
     * 包含年月日时分
     */
    @SuppressLint("SimpleDateFormat")
    public static String getInDetailTime(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String time = df.format(date);
        return time;
    }

    /**
     * 只包含时分秒
     */
    @SuppressLint("SimpleDateFormat")
    public static String getTime() {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        String time = df.format(new Date());
        return time;
    }

    /**
     * 生日
     */
    @SuppressLint("SimpleDateFormat")
    public static String getTime(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String time = df.format(date);
        return time;
    }


    public static String getTimeHHMM(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String time = df.format(date);
        return time;
    }

    /**
     * 生日
     */
    @SuppressLint("SimpleDateFormat")
    public static Date getDate(String time) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;
        try {
            date = df.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Long dateToStamp123(String s) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(s);
        } catch (ParseException e) {

        }
        return date.getTime() / 1000;
    }

    /**
     * 生日
     */
    @SuppressLint("SimpleDateFormat")
    public static Date getDateSS(String time) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = df.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 当天稍后2小时
     *
     * @return
     */
    public static String getNowTwoLaterDate() {
        String mYear;
        String mMonth;
        String mDay;
        String hour;
        String minute;
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
        mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        hour = String.valueOf(c.get(Calendar.HOUR_OF_DAY) + 2);
        minute = String.valueOf(c.get(Calendar.MINUTE));
        return mYear + "-" + mMonth + "-" + mDay + " " + hour + ":" + minute;
    }

    /**
     * 获取周五的日期
     *
     * @return
     */
    public static String getFridayNightDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }

        cal.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一

        int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);//根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, 4);

        return df.format(cal.getTime()) + " 19:00";
    }

    /**
     * 获取系统时间：xxxx-xx-xx xx:xx
     */
    public static String getStart100Time() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        return year - 50 + "-" + month + "-" + day;

    }

    /**
     * 获取系统时间：xxxx-xx-xx xx:xx
     */
    public static String getStartTimeandHHMM() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        return year + "-" + month + "-" + day + " " + hour + ":" + minute;

    }

    /**
     * 可选最晚日期
     */
    public static String getEndTimeandHHMM() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);

        return year + 10 + "-" + month + "-" + day + " " + hour + ":" + minute;

    }

    /**
     * 获取系统时间：xxxx-xx-xx xx:xx
     */
    public static String getStartTime() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        return year + "-" + month + "-" + day;

    }

    /**
     * 可选最晚日期
     */
    public static String getEndTime() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);

        return year + "-" + month + 3 + "-" + day + " " + hour + ":" + minute;

    }

    /**
     * 获取周六的日期
     *
     * @return
     */
    public static String getStatNightDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }

        cal.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一

        int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);//根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, 5);

        return df.format(cal.getTime()) + " 19:00";
    }

    /**
     * 当天中午
     */
    @SuppressLint("SimpleDateFormat")
    public static Date getPresentSysNoontime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;
        try {
            date = df.parse(getTime(new Date()) + " 12:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 当天中午
     */
    @SuppressLint("SimpleDateFormat")
    public static Date getDatetime(String time) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = df.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 当天当时
     */
    @SuppressLint("SimpleDateFormat")
    public static Date getPresentSysNowtime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;
        try {
            date = df.parse(getInDetailgetTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取明天晚上的日期
     *
     * @return
     */
    public static String getTomoreaoyDate() {

        String mYear;
        String mMonth;
        String mDay;

        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
        mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + 1);// 获取当前月份的日期号码

        return (mYear + "-" + mMonth + "-" + mDay + " 19:00");
    }


    /**
     * 当天晚上
     */
    @SuppressLint("SimpleDateFormat")
    public static Date getPresentSysNighttime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;
        try {
            date = df.parse(getTime(new Date()) + " 19:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static boolean compareDate(Date d1, Date d2) {
        if (d1.getTime() > d2.getTime()) {
            return true;
        } else if (d1.getTime() < d2.getTime()) {
            return false;
        } else {//相等
            return false;
        }
    }

    /**
     * 系统单年月日
     */
    @SuppressLint("SimpleDateFormat")
    public static String getPresentSystime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");// 设置日期格式
        String a = df.format(new Date());
        return a;
    }


    /**
     * 判断此时是否已经超过当天中午
     *
     * @return
     */
    public static boolean noonOut() {
        return compareDate(getPresentSysNowtime(), getPresentSysNoontime());
    }

    /**
     * 判断此时是否已经超过当天晚上
     *
     * @return
     */
    public static boolean nightOut() {
        return compareDate(getPresentSysNowtime(), getPresentSysNighttime());
    }

    /**
     * 判断此时是否已经超过当周周五晚上
     *
     * @return
     */
    public static boolean fridaytout() {
        return compareDate(getPresentSysNowtime(), getDate(getFridayNightDate()));
    }

    /**
     * 判断此时是否已经超过当周周六天晚上
     *
     * @return
     */
    public static boolean satdaytout() {
        return compareDate(getPresentSysNowtime(), getDate(getStatNightDate()));
    }

    /**
     * 获取当前年月日／星期几
     *
     * @return
     */
    public static String getWeekToDay() {
        String mWay;
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        return mWay;
    }

    public static String getWeek(String time) {
        String mWay;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        calendar.setTime(getDate(time));
        mWay = String.valueOf(calendar.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "天";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return "星期" + mWay;
    }

    public static String getTurnWeekDate(String time) {
        String mYear;
        String mMonth;
        String mDay;
        String mWay;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        calendar.setTime(getDate(time));
        mYear = String.valueOf(calendar.get(Calendar.YEAR)); // 获取当前年份
        mMonth = String.valueOf(calendar.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        mWay = String.valueOf(calendar.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "天";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return mYear + "年" + mMonth + "月" + mDay + "日" + " 星期" + mWay;
    }

    public static String getRecordDate(String time) {
        String mYear;
        String mMonth;
        String mDay;
        String mWay;
        String minute;
        String hour;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        calendar.setTime(getDate(time));
        mYear = String.valueOf(calendar.get(Calendar.YEAR)); // 获取当前年份
        mMonth = String.valueOf(calendar.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        mWay = String.valueOf(calendar.get(Calendar.DAY_OF_WEEK));
        hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
        minute = String.valueOf(calendar.get(Calendar.MINUTE));
        if ("1".equals(mWay)) {
            mWay = "天";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return mMonth + "月" + mDay + "日" + " 星期" + mWay + " ";

    }

    /**
     * 获取当前年月日／星期几
     *
     * @return
     */
    public static String getWeekDate() {
        String mYear;
        String mMonth;
        String mDay;
        String mWay;
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
        mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "天";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return mYear + "年" + mMonth + "月" + mDay + "日" + " 星期" + mWay;
    }


    /**
     * 时间戳转换
     */
    @SuppressLint("SimpleDateFormat")
    public static String timeConvert(long mill) {
        Date date = new Date(mill * 1000L);
        String strs = "";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            strs = sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strs;
    }

    public static String getDeviceUUID(Context context) {
        String uuid = null;
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        uuid = tm.getDeviceId();
        return uuid;
    }

    @SuppressLint("SimpleDateFormat")
    public static String getSystimeHHMM() {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");// 设置日期格式
        String a = df.format(new Date());
        return a;
    }

    /*
       * 将时间转换为时间戳
       */
    public static Long dateToStamp(String s) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = format.parse(s);
        return date.getTime() / 1000;
    }
}
